// DrawCurveCtrler.cpp: implementation of the CDrawCurveCtrler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "../Resource.h"
#include "DrawCurveCtrler.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDrawCurveCtrler::CDrawCurveCtrler(COLORREF color, int width, long style)
: m_lStartx(0), m_lStarty(0), m_lEndx(0),  m_lEndy(0)
{
	SetColor (color);		// set the pen color
	SetPenWidth (width);	// set the pen width
	SetPenStyle (style);	// set the pen style

	m_TaskTyle = ID_CURVE;	// a curve
}

CDrawCurveCtrler::~CDrawCurveCtrler()
{
}

void CDrawCurveCtrler::MoveAt(CDC *pDC, long x, long y)
{
	//
	// Set the CPen //////////////////////////////////////////
	//
	COLORREF XorColor = pDC->GetBkColor() ^ GetColor();
	CPen pen (GetPenStyle(), GetPenWidth(), XorColor);		// create a pen
	int oldmode = pDC->GetROP2();
	CPen* oldpen = pDC->SelectObject(&pen);

	// Set the Raster Operation To R2_XORPEN (dest = src XOR dest)
	pDC->SetROP2(R2_XORPEN); 

	//
	// Draw the Line in MoveAt() :
	//

	// delete the original image
	DrawArc (pDC, m_lStartx, m_lStarty, m_lEndx, m_lEndy);
	
	//
	// Reset the coordinaton.
	//
	m_lEndx = x;
	m_lEndy = y;

	// using the new coordination to redraw the image.
	DrawArc (pDC, m_lStartx, m_lStarty, m_lEndx, m_lEndy);

	// recover the DC :
	pDC->SetROP2(oldmode);
	pDC->SelectObject(oldpen);
	pen.DeleteObject();
}

void CDrawCurveCtrler::DrawObject(CDC* pDC)
{
	//
	// Set the CPen //////////////////////////////////////////
	//
	CPen pen(GetPenStyle(), GetPenWidth(), GetColor());
	int oldmode = pDC->GetROP2();
	CPen* oldpen = pDC->SelectObject(&pen);

	// recorde the draw rect with logical coordination :
	m_DrawRect.left = min(m_lStartx, m_lEndx) - GetPenWidth();
	m_DrawRect.right = max(m_lStartx, m_lEndx) + GetPenWidth();
	m_DrawRect.top = min(m_lStarty, m_lEndy) - GetPenWidth();
	m_DrawRect.bottom = max(m_lStarty, m_lEndy) + GetPenWidth();

	// complete drawing the line.
	DrawArc (pDC, m_lStartx, m_lStarty, m_lEndx, m_lEndy);

	// recover the DC.
	pDC->SetROP2(oldmode);
	pDC->SelectObject(oldpen);
	pen.DeleteObject();
}

void CDrawCurveCtrler::StartPoint(long x, long y)
{
	m_lStartx = m_lEndx = x;
	m_lStarty = m_lEndy = y;
}

void CDrawCurveCtrler::DrawArc(CDC *pDC, long x1, long y1, long x2, long y2)
{
	long minx =	min(x1, x2);
	long miny = min(y1, y2);
	long maxx = max(x1, x2);
	long maxy = max(y1, y2);

	pDC->Arc (minx, miny, maxx, maxy, x1, y1, x2, y2);
}
